package com.tnovoselec.android.common.util;

import java.io.File;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;


public class ApplicationUtil {

    private ApplicationUtil() {
	super();
    }

    /**
     * Get device's phone number
     * 
     * @param context
     * @return phone number as String
     */
    public static String getPhoneNumber(Context context) {
	TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	return telephonyMgr.getLine1Number();
    }

    /**
     * Get device's identifier
     * 
     * @param context
     * @return device's identifier as String
     */
    public static String getDeviceIdentifier(Context context) {
	TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	return telephonyMgr.getDeviceId();
    }

    /**
     * Get SIM card identifier
     * 
     * @param context
     * @return SIM card identifier as String
     */
    public static String getSimIdentifier(Context context) {
	TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
	return telephonyMgr.getSimSerialNumber();
    }

    /**
     * Get user's identifier
     * 
     * @param context
     * @return user's identifier as String
     */
    public static String getUserIdentifier(Context context) {
	String identifier = ApplicationUtil.getPhoneNumber(context);
	if (TextUtils.isEmpty(identifier)) {
	    identifier = ApplicationUtil.getDeviceIdentifier(context);
	    if (TextUtils.isEmpty(identifier)) {
		identifier = ApplicationUtil.getSimIdentifier(context);
	    }
	}

	return identifier;
    }

    /**
     * This method checks if GPS service is enabled on device
     * 
     * @param context
     * @return boolean flag if GPS is enabled
     */
    public static boolean isGpsEnabled(Context context) {
	LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
	return locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    /**
     * This method checks if device is connect to some access point
     * 
     * @param context
     * @return boolean flag if internet connection is available
     */
    public static boolean isOnline(Context context) {
	ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	return (cm.getActiveNetworkInfo() != null) && cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }

    public static String getExternalStorage(Context context) {
	if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
	    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
		Log.i("External files dir: ", context.getExternalFilesDir(null).getAbsolutePath());
		return context.getExternalFilesDir(null).getAbsolutePath();
	    }
	    return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + context.getPackageName();
	} else {
	    return context.getFilesDir().getAbsolutePath();
	}
    }
}
